package org.jeecgframework.core.util.clean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jeecg.business.entity.site.SiteEntity;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.code.CodeUtil;
import org.jeecgframework.core.util.http.HttpClientUtil;

public class SinaWeiboHtmlCleanUtil {
	/**
	 * 
	 * <p>
	 * 方法简要描述: 模拟登录，获取cookies
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version 
	 * @see
	 * @param username
	 * @param passwrod
	 * @return
	 */
	public static Map<String , String> loginSinaBlog4Html(String username , String passwrod , String charset){
		String cookies = null;
		String loginUrl = "https://passport.weibo.cn/sso/login";//https://passport.weibo.cn/sso/login
		DefaultHttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, HttpClientUtil.timeout); 
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, HttpClientUtil.timeout);
		HttpPost post = null;
		try {
			post = new HttpPost(loginUrl);
			post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E)");
			post.addHeader("Accept", "*/*");
			post.addHeader("Connection", "Keep-Alive");
			post.addHeader("Accept-Encoding", "gzip, deflate");
			post.addHeader("Accept-Language", "zh-cn");
			post.addHeader("Host","passport.weibo.cn");
			post.addHeader("Origin","https://passport.weibo.cn");
			post.addHeader("Referer","https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F");
			
			
			List<NameValuePair> paramlist = new ArrayList<NameValuePair>();
			paramlist.add(new BasicNameValuePair("username" , username));
			paramlist.add(new BasicNameValuePair("password" , passwrod));
			paramlist.add(new BasicNameValuePair("savestate" , "1"));
			paramlist.add(new BasicNameValuePair("ec" , "0"));
			paramlist.add(new BasicNameValuePair("pagerefer" , "https://passport.weibo.cn/signin/welcome?entry=mweibo"));
			paramlist.add(new BasicNameValuePair("entry" , "mweibo"));
			post.setEntity(new UrlEncodedFormEntity(paramlist, charset));
			/* post data
			username:lin2528403@tom.com
			password:123456a
			savestate:1
			ec:0
			pagerefer:https://passport.weibo.cn/signin/welcome?entry=mweibo
			entry:mweibo
			*/
			
			HttpResponse resp5 = client.execute(post);
			return HttpClientUtil.getAllHeader(resp5);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (post != null)
				post.releaseConnection();
		}
		return null;
	}
	
	public static String fetchSinaBlogOld(String html){
		HtmlParseResult result = null;
		try{
			HtmlCleaner cleaner = new HtmlCleaner();
			result = new HtmlParseResult();
			
			CleanerProperties props = cleaner.getProperties();
			props.setUseCdataForScriptAndStyle(true);
			props.setRecognizeUnicodeChars(true);
			props.setUseEmptyElementTags(true);
			props.setAdvancedXmlEscape(true);
			props.setTranslateSpecialEntities(true);
			props.setBooleanAttributeValues("empty");
			TagNode node = cleaner.clean(html);
			
//			System.out.println(url);
			
			//提取信息字段
			String str = new PrettyXmlSerializer(props).getAsString(node);
//			System.out.println(str);
			int f = str.indexOf("\"pid\":\"pl_weibo_direct\"");
			int e = str.indexOf("\"})",f);
			System.out.println(f+" "+e);
			String nstr = str.substring(f,e);
			int hf = nstr.indexOf("\"html\":\"");
			String lstr = nstr.substring(hf+"\"html\":\"".length());
			
			String mystr = lstr;
			
			//转换%
			mystr = mystr.replaceAll("\\%", "bai-fen-hao");		
			
			mystr = CodeUtil.unescape(mystr.replaceAll("\\\\u", "\\%u"));
			
			mystr = mystr.replaceAll("bai-fen-hao","\\%");	
			
			//
			
//			mystr = mystr.replaceAll("\\n", "");
			
			mystr = mystr.replaceAll("\\\\/", "/");
			mystr = mystr.replaceAll("\\\\n", "");
			mystr = mystr.replaceAll("\\\\ \"", "\"").replaceAll("\\\\\"", "\"");
			mystr = mystr.replaceAll("<dd","<div");
			mystr = mystr.replaceAll("</dd","</div");
			mystr = mystr.replaceAll("<dl","<div");
			mystr = mystr.replaceAll("</dl","</div");
			mystr = mystr.replaceAll("<dt","<div");
			mystr = mystr.replaceAll("</dt","</div");
			
			node = cleaner.clean("<html><body>"+mystr+"</body></html>");
			
			String xpath = "//div[@class='content']";
			
			Object[] ns=node.evaluateXPath(xpath);	
			
			return mystr;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static HtmlParseResult fetchSinaBlog4Html(String mystr , String charset,SiteEntity site , String cookies){
		HtmlParseResult result = null;
		try{
			result = new HtmlParseResult();
			
			mystr = mystr.replaceAll("\\%", "bai-fen-hao");		
			mystr = CodeUtil.unescape(mystr.replaceAll("\\\\u", "\\%u"));
			mystr = mystr.replaceAll("bai-fen-hao","\\%");
			
			JSONObject jobj = JSONObject.fromObject(mystr);
			int maxpage = jobj.getInt("maxPage");
			result.setMaxPage(maxpage);
			JSONArray cards = jobj.getJSONArray("cards");
			JSONObject card = null;
			HtmlParseResult each = null;
			if(cards != null){
				String cardTypeName = null;
				for(int i=0;i<cards.size();i++){
					card = cards.getJSONObject(i);
					cardTypeName = card.getString("card_type_name");
					if("微博".equals(cardTypeName)){
						JSONArray cardGroup = card.getJSONArray("card_group");
						JSONObject eachCard = null;
						for(int j=0;j<cardGroup.size();j++){
							eachCard = cardGroup.getJSONObject(j);
							cardTypeName = eachCard.getString("card_type_name");
							if("微博".equals(cardTypeName)){
								String scheme = eachCard.getString("scheme");
								JSONObject mblog = eachCard.getJSONObject("mblog");
								String created_at = mblog.getString("created_at");
								if(!created_at.startsWith("201"))	created_at = "2015-"+created_at;
								created_at += ":00";	//加上秒数
								String text = mblog.getString("text");
								text = StringUtil.removeHTMLLableExe(text);
								/**
								 * 获取用户信息
								 */
								JSONObject user = mblog.getJSONObject("user");
								String zz = user.getString("screen_name");
								String txlj = user.getString("profile_image_url");
								/**
								 * 点击数
								 */
								int zfs = mblog.getInt("reposts_count");
								int pls = mblog.getInt("comments_count");
								int dzs = mblog.getInt("attitudes_count");
								
								
								each = new HtmlParseResult();
					            each.setBodycontent(text);
					            each.setAllcontent(text);
					            each.setType(site.getJclx());
					            each.setUrl(scheme);
					            each.setEventDate(DataUtils.parseDate(created_at));
					            each.setZz(zz);
//					            each.setTxlj(txlj);
//					            each.setZfs(zfs);
//					            each.setPls(pls);
//					            each.setDzs(dzs);
					            result.addChildResult(each);
							}
						}
					}
				}
			}
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return result;
	}
	
	public static HtmlParseResult fetchSinaBlog(String url ,String charset,SiteEntity site , String cookies){
		HtmlCleaner cleaner = new HtmlCleaner();
		String str = HttpClientUtil.getSourceFromUrl(url, site.getWyzfj(), cookies);
		int f = 0;	//执行3次
		while(str.contains("\"ok\":0") && f < 3){	//不成功
			f++;
			str = HttpClientUtil.getSourceFromUrl(url, site.getWyzfj(), cookies);
		}
		return fetchSinaBlog4Html(str, charset, site, cookies);
	}
	
	public static void main(String[] args) {
		Map<String , String> headers = loginSinaBlog4Html("lin2528403@tom.com","123456a","utf-8");
		//http://s.weibo.com/weibo/%25E8%258D%25A3%25E6%2588%2590%25E5%25BB%25BA%25E7%25AD%2591%25E5%2585%25AC%25E5%258F%25B8&nodup=1&page=2
		String shouji_url = "http://m.weibo.cn/page/pageJson?containerid=100103type%3D1%26q%3D%25E6%2589%25A7%25E6%25B3%2595%25E5%25B1%2580&type=all&page=1";
		String web_url = "http://s.weibo.com/weibo/%25E8%258D%25A3%25E6%2588%2590%25E5%25BB%25BA%25E7%25AD%2591%25E5%2585%25AC%25E5%258F%25B8&nodup=1&page=1";
		String encode = "utf-8";
		
		
		String  cookies = "ALF=1451802975; " +
							"SUS=SID-3267503754-1449210975-XD-bmi6e-131f7c359c36b92dfb963b98f0cd275e; " +
							"SUE=es%3D16ce46862e79e83a1d8e6c2ff4e0a7b2%26ev%3Dv1%26es2%3D884a62830bb025efa976c33a912993e8%26rs0%3DpJKvxs7LQzfGI1wyqF%252FKQk4nJAg8F9HSh9l94rFJ8cs0ZIG8qvz9pmsM6XC3ZoknSP7pW%252BTqpzKY70lHLQXEWHCyRthgby6gXJMWLGaLT0VGcRrHZGJRcyE6dfYoLZ2kPypflTNWYD9Py55QgS48wNLi6wjYKNdHW05jIdNY%252BKw%253D%26rv%3D0; " +
							"SUP=cv%3D1%26bt%3D1449210975%26et%3D1449297375%26d%3Dc909%26i%3D275e%26us%3D1%26vf%3D%26vt%3D%26ac%3D%26st%3D0%26uid%3D3267503754%26name%3Dlin2528403%2540tom.com%26nick%3D123dsa%26fmp%3D%26lcp%3D; " +
							"SUB=_2A257ZUQPDeTxGeVM7VUU8C3LzjiIHXVYpmxHrDV8PUJbrdAPLUKlkW1xRryri4FFo15OqMnRbHNJf7O0WA..; " +
							"SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5ei7EJDJoSioEZX_x_jCMx5JpX5oz7; NSC_wjq_txfjcp_mjotij=ffffffff094113d545525d5f4f58455e445a4a423660; " +
							"WBStore=5955be0e3d5411da|undefined; _s_tentry=-;  ";
		String html = HttpClientUtil.getSourceFromUrl(web_url , encode , cookies);
		System.out.println(fetchSinaBlogOld(html));
	}
}
